/**
 * 
 */
package sailing.managers.impl;

import java.util.List;

import sailing.dao.AccountingSubjectDao;
import sailing.entity.AccountingSubject;
import sailing.entity.SubjectCategory;
import sailing.managers.AccountingSubjectManager;
import sailing.ui.systemsetting.model.SubjectTreeNode;
import cjlite.annotations.Inject;
import cjlite.utils.Lists;

/**
 * @author kevin
 * 
 */
public class AccountingSubjectManagerImpl implements AccountingSubjectManager {

	private List<AccountingSubject> asList = Lists.newArrayList();

	@Inject
	private AccountingSubjectDao asDao;

	/*
	 * (non-Javadoc)
	 * 
	 * @see sailing.managers.AccountingSubjectManager#buildSubjectTree(sailing.ui.systemsetting.model.SubjectTreeNode)
	 */
	@Override
	public void buildSubjectTree(SubjectTreeNode root) {
		asList.clear();
		asList.addAll(asDao.getAllSubjectList());

		for (SubjectCategory sc : SubjectCategory.getSubjectCateList()) {
			SubjectTreeNode stn = new SubjectTreeNode(sc.getName(), sc);
			this.buildChild(stn, sc, asList);
			root.addChild(stn);
		}
		// root.addChild()
	}

	private void buildChild(SubjectTreeNode stn, SubjectCategory sc, List<AccountingSubject> asList) {
		for (AccountingSubject as : asList) {
			if (as.getParentId().equals(String.valueOf(sc.getCode()))) {
				SubjectTreeNode child = new SubjectTreeNode(as.getName(), as);
				stn.addChild(child);
			}
		}
	}

	@Override
	public List<SubjectCategory> getSubjectCategoryList() {
		return SubjectCategory.getSubjectCateList();
	}

	@Override
	public boolean save(AccountingSubject as) {
		return asDao.save(as);
	}

	@Override
	public List<AccountingSubject> getSubjectListByCateId(int code) {
		return asDao.getSubjectListByCateId(code);
	}

	@Override
	public boolean update(AccountingSubject as) {
		return asDao.update(as);
	}

	@Override
	public boolean delete(AccountingSubject as) {
		return asDao.delete(as);
	}
}
